<?php
/**
 * customer 后台整体妆容的管理
 * Author: lyun
 * Date: 2018/5/17
 * Created by Panxsoft.
 */


namespace App\Http\Controllers\Admin\Customer;


use App\Http\Controllers\ApiController;

use App\Http\Requests\Admin\Customer\Store\PackIndexRequest;
use App\Http\Requests\Admin\Customer\Store\PackPatchRequest;
use App\Models\Store\Pack;
use App\Services\Repositories\Pack\PackService;
use App\Services\Repositories\Pack\PackCategoryRepository;
use App\Services\Repositories\Pack\PackRepository;
use App\Services\Utils\Guard;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Pagination\LengthAwarePaginator;

class PackController extends ApiController
{
    const ROUTES = [
        'index'   => 'api.admin.customer.pack.index',
        'destroy' => 'api.admin.customer.pack.delete',
        'update'  => 'api.admin.customer.pack.update',
    ];

    const DEFAULT_ORDER_BYS = [
        ['order_by' => 'created_at', 'sort' => 'desc'],
    ];

    private $packRepository;
    private $packService;
    private $packCategoryRepository;

    public function __construct(PackRepository $packRepository,
                                PackService $packService,
                                PackCategoryRepository $packCategoryRepository
    )
    {
        $this->packRepository = $packRepository;
        $this->packService = $packService;
        $this->packCategoryRepository = $packCategoryRepository;
    }
    
    /**
     * Display a listing of the resource.
     * @test
     *
     * @param PackIndexRequest $request
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function index(PackIndexRequest $request)
    {
        $this->checkCustomer();

        /** @var LengthAwarePaginator|Pack[] $packs */
        $packs = $this->packRepository->getPackPaginate($request);

        /** @var Collection $pack_collect */
        $pack_collect = $packs->getCollection();

        $this->packService->transformPackAssetsProducts($pack_collect);

        $customer_id = Guard::customerAdmin()->customer_id;

        $pack_categories = $this->packCategoryRepository->searchByCustomerId($customer_id);

        return $this->response(compact('packs', 'pack_categories'));
    }

    /**
     * @test
     * @param PackPatchRequest $request
     * @param $pack_id
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function update(PackPatchRequest $request, $pack_id)
    {
        $this->checkCustomer();

        /** @var Pack $pack */
        $pack = (new Pack)
            ->where('customer_id', Guard::customerAdmin()->customer_id)
            ->where('store_id', Pack::STORE_ID_CUSTOMER)
            ->find($pack_id);
        $this->checkItemExist($pack, trans('admin.pack'));

        $data = $this->packService->assemblePack($request);

        $updated = $pack->update($data);

        return $this->response(compact('updated'));
    }

    /**
     * 删除整体妆容
     * @test
     *
     * @param $pack_id
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function destroy($pack_id)
    {
        $this->checkCustomer();

        /** @var Pack $pack */
        $pack = (new Pack)
            ->where('customer_id', Guard::customerAdmin()->customer_id)
            ->where('store_id', Pack::STORE_ID_CUSTOMER)
            ->find($pack_id);
        $this->checkItemExist($pack, trans('pc.pack'));

        $deleted = PackRepository::deletePack($pack);

        return $this->response(compact('deleted'));
    }
}
